<template>
  <div class="app-container">
    <!-- 搜索条件 -->
    <div class="filter-container">
      <el-input
        @keyup.enter.native="handleFilter"
        style="width: 200px"
        class="filter-item"
        placeholder="卷名"
        v-model="keyword"
      >
      </el-input>

      <!-- <el-select clearable class="filter-item" style="width: 130px" v-model="listQuery.type" placeholder="类型">
        <el-option v-for="item in  typeOptions" :key="item.key" :label="item.display_name+'('+item.key+')'" :value="item.key">
        </el-option>
      </el-select> -->

      <el-button
        class="filter-item"
        type="primary"
        v-waves
        icon="search"
        @click="handleFilter"
        >搜索</el-button
      >
      <el-button
        class="filter-item"
        type="primary"
        @click="handleCreate"
        icon="edit"
        >添加</el-button
      >

      <el-button
        class="filter-item"
        type="primary"
        @click="handleDelAll"
        icon="edit"
        >批量删除</el-button
      >
    </div>

    <!-- 表格 v-loading.body="listLoading"-->
    <el-table
      ref="multipleTable"
      @selection-change="handleSelectionChange"
      :data="rolls"
      border
      fit
      highlight-current-row
    >
      <el-table-column type="selection" width="50"> </el-table-column>

      <el-table-column align="center" label="序号" width="70">
        <template slot-scope="scope">
          {{ scope.$index  }}
        </template>
      </el-table-column>

      <el-table-column label="书名" width="" align="center" prop="title">
        <template slot-scope="scope">
          {{ scope.row.novel_title }}
        </template>
      </el-table-column>

      <el-table-column label="卷名" width="" align="center">
        <template slot-scope="scope">
          <router-link :to="`chaptersManage/chapterList?roll_id=${scope.row.roll_id}`">{{
            scope.row.roll_title
          }}</router-link>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column> 
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="listQuery.currPage"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="listQuery.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 新增弹窗 -->
    <el-dialog title="新增卷" :visible.sync="addDialog" @close="closeDialog">
      <el-form
        class="small-space"
        :model="roll"
        label-position="left"
        label-width="70px"
        style="width: 400px; margin-left: 50px"
      >
        <el-form-item label="卷名">
          <el-input v-model="roll.roll_title"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialog = false">取 消</el-button>

        <el-button type="primary" @click="handleCreateSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 弹框
      dialogVisible: false,
      rolls: [
      ],
      roll:{
        roll_title:''
      },
      // list: null,
      // listLoading: true,
        editDialog:false,
        addDialog:false,

      keyword:'',
      list: null, //表格list
      total: null,
      listLoading: true,
      novel_id: null,
      listQuery: {
        currPage: 1,
        pageSize: 10,
      },
      is_edit:false,
      multipleSelection: [],
    };
  },
   async created(){
      this.novel_id = this.$route.query.novel_id;
      console.log(this.novel_id);
      this.getList()
  },
  methods: {
    //获取列表数据
    async getList() {
       let res = await this.$http.get('/rolls',{params:{novel_id: this.novel_id, pageSize:this.listQuery.pageSize, currPage:this.listQuery.currPage}})
      this.rolls = res.data.rolls;
      this.listQuery.currPage = parseInt(res.data.currPage)
      this.listQuery.pageSize = parseInt(res.data.pageSize)
      this.total = res.data.total
    },
    //编辑
    handleEdit(index, row) {
      this.addDialog = true;
      this.is_edit = true; //处于打开对话框状态
      this.roll = row
      console.log("编辑的row：", index, "-----", row);
    },
    //单个删除
    handleDelete(index, row) {

        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.delFun(row)
           //前端删除。
          this.rolls.splice(index, 1);
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    delFun(row) {
      this.$http.delete('/delete_roll',{params:{roll_id :row.roll_id}}).then(res=>{})
    },
    //批量删除
    handleDelAll() {
      var vm = this;
      console.log("批量删除选择的row：", vm.multipleSelection);
       this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定批量删除",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
          //前端依次执行单个删除操作
           vm.rolls = vm.rolls.filter((item)=>{
              for(let selection of vm.multipleSelection){
                if(item.roll_id === selection.roll_id){
                  return false
                }
              }
              return true;
            })
          for (var index in vm.multipleSelection) {
              console.log('======',vm.multipleSelection[index])
              vm.delFun(vm.multipleSelection[index]);
          };
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
    },
    //搜索
    async handleFilter() {
      let res = await this.$http.get('/search_roll',{params:{novel_id: this.novel_id, keyword:this.keyword, pageSize:this.listQuery.pageSize, currPage:this.listQuery.currPage}})
      this.rolls = res.data.rolls;
      this.listQuery.currPage = parseInt(res.data.currPage)
      this.listQuery.pageSize = parseInt(res.data.pageSize)
      this.total = res.data.total
      console.log(res.data);
    },
    //操作分页
    handleSizeChange(val) {
      console.log("--------一页多少条：", val);
      this.listQuery.pageSize = val;
      this.getList();
    },
    //操作分页
    handleCurrentChange(val) {
      console.log("--------当前页：", val);
      this.listQuery.currPage = val;
      this.getList();
    },
    //新增
    handleCreate() {
      this.addDialog = true;
    },
    // 监听对话框的关闭
    closeDialog(){
      this.is_edit = false;
      this.roll = {}
    },
    //新增提交
   async  handleCreateSubmit() {
     let res;
     if(!this.is_edit){
         res = await  this.$http.post('/insert_roll',{roll_title:this.roll.roll_title, novel_id:this.novel_id})
     } else{
        res = await  this.$http.put('/update_roll',this.roll)
     }
    
      this.$message({
        type:res.data.type,
        message:res.data.message
      })
      this.getList()
      this.addDialog = false; 
      //这里作演示用，正式新增 请提交到接口
    //   vm.list.push(vm.temp);
    //   console.log("新增后", vm.list);

    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
  },
};
</script>
<style >
  a:hover{
    color:rgb(237, 66, 89);
  }
</style>
